function getTarget(node) {
  if (node === void 0) {
    return document.body;
  }
  if (typeof node === "string" && node.indexOf("?") === 0) {
    return document.body;
  } else if (typeof node === "string" && node.indexOf("?") > 0) {
    node = node.split("?")[0];
  }
  if (node === "body" || node === true) {
    return ducument.body;
  }
  console.log(node);
  return node instanceof window.Node ? node : document.querySelector(node);
}

function getShouldUpdate(node) {
  if (!node) {
    return false;
  }
  if (typeof node === "string" && node.indexOf("?") > 0) {
    try {
      const config = JSON.parse(node.split("?")[1]);
      return config.autoUpdate || false;
    } catch (e) {
      return false;
    }
  }
}

const transfer = {
  inserted(el, { value }, vnode) {
    el.className = el.className
      ? `${el.className} v-transfer-dom`
      : "v-transfer-dom";
    const parentNode = el.parentNode;
    const comment = document.createComment("");
    let hasMoveOut = false;
    if (value !== false) {
      parentNode.replaceChild(comment, el);
      getTarget(value).appendChild(el);
      hasMoveOut = true;
    }
    if (!el._transformDomData) {
      el._transformDomData = {
        parentNode: parentNode,
        comment: comment,
        traget: getTarget(value),
        hasMovedOut: hasMoveOut,
      };
    }
  },
  componentUpdated(el, { value }) {
    const shouldUpdate = getShouldUpdate(value);
    if (!shouldUpdate) return;
    const $ref1 = el._transformDomData;
    const { parentNode, comment, hasMoveOut } = $ref1;
    if (!hasMoveOut && value) {
      parentNode.replaceChild(comment, el);
      getTarget(value).appendChild(el);
      el._transferDomData = Object.assign({}, el._transformDomData, {
        hasMovedOut: true,
        traget: getTarget(value),
      });
    } else if (hasMoveOut && value === false) {
      parentNode.replaceChild(el, homde);
      el._transferDomData = Object.assign({}, el._transferDomData, {
        hasMoveOut: false,
        traget: getTarget(value),
      });
    } else if (value) {
      getTarget(value).appendChild(el);
    }
  },
  unbind(el, binding) {
    el.className = el.className.replace("v-transfer-dom", "");
    if (el._transferDomData && el._transferDomData.hasMoveOut) {
      el._transferDomData.parentNode &&
        el._transferDomData.parentNode.appendChild(el);
    }
    el._transferDomData = null;
  },
};
export default transfer;
